package com.hsytech.dijiebao.home.alldetail;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.account.SignInActivity;
import com.hsytech.dijiebao.base.BaseActivity;
import com.hsytech.dijiebao.utils.Constants;
import com.hsytech.dijiebao.utils.HttpUtil;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;
import com.hsytech.dijiebao.view.CircleImageView;
import com.squareup.picasso.Picasso;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import io.rong.imkit.RongIM;

public class HotelDetailActivity extends BaseActivity {
    private ImageButton top_back_btn;
    private CheckBox bar_right_top_btn;
    private TextView bar_title_tv;
    private LinearLayout chart;
    private String ID;
    private TextView create_datetime1, hotel_cat_name1, contact_name1, contact_phone1, start_time1, check_in_num1,
            content1, area_name1;
    private String contact_name, contact_phone;
    private CircleImageView circleImageView;
    private String uid = "", publisher_name = "";
    private Boolean isCollected = false;
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            JSONObject result = null;
            try {
                result = new JSONObject(response);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JSONObject myData = result.optJSONObject("data");

            String create_datetime = myData.optString("create_datetime");

            String hotel_cat_name = myData.optString("hotel_cat_name");

            contact_name = myData.optString("contact_name");
            contact_phone = myData.optString("contact_phone");

            String start_time = myData.optString("start_time");
            String end_time = myData.optString("end_time");

            String check_in_num = myData.optString("check_in_num");

            String content = myData.optString("content");

            String area_name = myData.optString("area_name");

            String city_name = myData.optString("city_name");

            String is_collection = myData.optString("is_collection");
            String head_photo1 = myData.optString("head_photo");
            publisher_name = myData.optString("publisher_name");
            Picasso.with(HotelDetailActivity.this).load("http://djb.dev.hsydev.com/Upload/image_sm/" + head_photo1)
                    .into(circleImageView);

            uid = myData.optString("uid");
            create_datetime1.setText(create_datetime);
            hotel_cat_name1.setText(hotel_cat_name);
            contact_name1.setText(contact_name);
            contact_phone1.setText(contact_phone);

            // String a = getStringToDate(start_time);
            // String b = getStringToDate(end_time);
            start_time1.setText(start_time + "��" + start_time);
            check_in_num1.setText(check_in_num);
            content1.setText(content);
            area_name1.setText(area_name + "  " + city_name);

            if (is_collection.equals("1")) {
                bar_right_top_btn.setChecked(true);
            } else {
                bar_right_top_btn.setChecked(false);
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {

        }
    };
    private String title, url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotel_detail);

        initView();
        getHotleIntentID();
        getHttp();
    }

    private void initView() {
        initTitle();
        initBody();
    }

    private void initBody() {

        circleImageView = (CircleImageView) findViewById(R.id.CircleImageView);
        create_datetime1 = (TextView) findViewById(R.id.create_datetime);
        hotel_cat_name1 = (TextView) findViewById(R.id.hotel_cat_name);
        contact_name1 = (TextView) findViewById(R.id.contact_name);
        contact_phone1 = (TextView) findViewById(R.id.contact_phone);
        start_time1 = (TextView) findViewById(R.id.start_time);

        check_in_num1 = (TextView) findViewById(R.id.check_in_num);
        content1 = (TextView) findViewById(R.id.content);
        area_name1 = (TextView) findViewById(R.id.area_name);
        // ��绰
        contact_phone1.setOnClickListener(new OnClickListener() {
            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                // ����һ����ͼ
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contact_phone));
                // ��ʼ��ͼ(������绰)
                if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    Activity#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for Activity#requestPermissions for more details.
                    return;
                }
                startActivity(intent);

            }
        });

        // ����
        chart = (LinearLayout) findViewById(R.id.chart);
        chart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (SharedPreferencesTools.getTOKEN(getApplicationContext()) == null) {
                    showToast(R.string.msg_no_have_token);
                    Intent intent = new Intent(HotelDetailActivity.this, SignInActivity.class);
                    startActivity(intent);
                } else {
                    if (RongIM.getInstance() != null) {
                        /**
                         * �������Ľ��档
                         *
                         * @param context
                         *            Ӧ�������ġ�
                         * @param targetUserId
                         *            Ҫ��֮������û� Id��
                         * @param title
                         *            ����ı��⣬��������ֵ����Ĭ����ʾ��֮������û����ơ�
                         */

                        if (!uid.equals("") && !publisher_name.equals("")) {

                            RongIM.getInstance().startPrivateChat(HotelDetailActivity.this, uid, publisher_name);
                        }

                    }
                }

            }
        });
    }

    private void getHotleIntentID() {
        Intent intent = getIntent();
        if (intent != null) {
//            if (intent.getStringExtra("collectHotle").equals("collectHotle")) {
//                bar_right_top_btn.setVisibility(View.GONE);
//                isCollected = true;
//            }
            ID = intent.getStringExtra("id");
            title = intent.getStringExtra("title");
            url = intent.getStringExtra("url");

        }
    }

    private void getHttp() {


        Map<String, String> map = new HashMap<>();
        map.put("id", ID);
        if (!TextUtils.isEmpty(SharedPreferencesTools.getTOKEN(this))) {
            map.put("token", SharedPreferencesTools.getTOKEN(this));
        }

        HttpUtil.httpPostWithParams(this, 0, Constants.HTTP_HOTLE, map, callBack);


//		RequestParams data = new RequestParams();
//		data.put("id", ID);
//		// showToast(ID);
//		data.put("token", SharedPreferencesTools.getTOKEN(this));
//		HttpUtils.post(Constants.HTTP_HOTLE, this, data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//				JSONObject result = data.optJSONObject("result");
//
//				JSONObject myData = result.optJSONObject("data");
//
//				String create_datetime = myData.optString("create_datetime");
//
//				String hotel_cat_name = myData.optString("hotel_cat_name");
//
//				contact_name = myData.optString("contact_name");
//				contact_phone = myData.optString("contact_phone");
//
//				String start_time = myData.optString("start_time");
//				String end_time = myData.optString("end_time");
//
//				String check_in_num = myData.optString("check_in_num");
//
//				String content = myData.optString("content");
//
//				String area_name = myData.optString("area_name");
//
//				String city_name = myData.optString("city_name");
//
//				String is_collection = myData.optString("is_collection");
//				String head_photo1 = myData.optString("head_photo");
//				publisher_name = myData.optString("publisher_name");
//				Picasso.with(HotelDetailActivity.this).load("http://djb.dev.hsydev.com/Upload/image_sm/" + head_photo1)
//						.into(circleImageView);
//
//				uid = myData.optString("uid");
//				create_datetime1.setText(create_datetime);
//				hotel_cat_name1.setText(hotel_cat_name);
//				contact_name1.setText(contact_name);
//				contact_phone1.setText(contact_phone);
//
//				// String a = getStringToDate(start_time);
//				// String b = getStringToDate(end_time);
//				start_time1.setText(start_time + "��" + start_time);
//				check_in_num1.setText(check_in_num);
//				content1.setText(content);
//				area_name1.setText(area_name + "  " + city_name);
//
//				if (is_collection.equals("1")) {
//					bar_right_top_btn.setChecked(true);
//				} else {
//					bar_right_top_btn.setChecked(false);
//				}
//
//			}
//
//			@Override
//			public void onError(String mess) {
//				super.onError(mess);
//				showToast(mess);
//			}
//		});
    }

    public static String getStringToDate(String time) {
        Date Time = new Date(Long.parseLong(time) * 1000);
        SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");

        // SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy/MM/dd");

        String retStrFormatNowDate = sdFormatter.format(Time).substring(5);
        return retStrFormatNowDate;
    }

    private void postHttp1() {
        // ����
//		RequestParams data = new RequestParams();
//		if (SharedPreferencesTools.getTOKEN(this) != null) {
//			data.put("token", SharedPreferencesTools.getTOKEN(this));
//		} else {
//			showToast("���ȵ�½");
//		}
//		data.put("type", 2);
//		data.put("tid", ID);
//		HttpUtils.post(Constants.HTTP_DEL_MYSELF, this, data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//				Toast.makeText(HotelDetailActivity.this, "ɾ���ɹ�", Toast.LENGTH_SHORT).show();
//			}
//
//			@Override
//			public void onError(String mess) {
//				// TODO Auto-generated method stub
//				super.onError(mess);
//				showToast(mess);
//			}
//
//		});
    }

    private void postHttp() {

//		RequestParams data = new RequestParams();
//		data.put("id", ID);
//		if (SharedPreferencesTools.getTOKEN(this) != null) {
//			data.put("token", SharedPreferencesTools.getTOKEN(this));
//		} else {
//			showToast("���ȵ�½");
//		}
//
//		HttpUtils.post(Constants.HTTP_COLLECT_HOTLE, data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//
//				showToast("���ղ�");
//			}
//
//			@Override
//			public void onError(String mess) {
//				// TODO Auto-generated method stub
//				super.onError(mess);
//				showToast(mess);
//			}
//
//		});
    }

    private void initTitle() {
        top_back_btn = (ImageButton) findViewById(R.id.top_back_btn);
        top_back_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        bar_right_top_btn = (CheckBox) findViewById(R.id.bar_right_top_btn);
        bar_right_top_btn.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                if (arg1) {
                    if (isCollected) {

                    } else {

                        postHttp();
                    }

                    // bar_right_top_btn.setChecked(false);
                } else {
                    postHttp1();

                    // bar_right_top_btn.setChecked(true);
                }
            }
        });
        // bar_right_top_btn.setOnClickListener(new OnClickListener() {
        //
        // @Override
        // public void onClick(View arg0) {
        // postHttp();
        // }
        // });
        bar_title_tv = (TextView) findViewById(R.id.bar_title_tv);
        bar_title_tv.setText("����Ƶ���Ϣ");

    }

}
